//
//  VipViewController.m
//  HQJFacilitator
//
//  Created by mymac on 16/9/20.
//  Copyright © 2016年 Fujian first time iot technology investment co., LTD. All rights reserved.
//

#import "VipViewController.h"
#import "ToolView.h"
#import "NoticeView.h"
#import "VipTableViewCell.h"
#import "VipViewModel.h"
#import "XTPopView.h"
#import "logonVipViewController.h"
#import "FacilitatorLogonViewController.h"

@interface VipViewController ()<UITableViewDelegate,UITableViewDataSource,selectIndexPathDelegate>
{
    
    NSInteger _scrollOffSize;
    
}
//@property (nonatomic,strong)UIView *noticeView;
@property (nonatomic,strong)UILabel *noticeLabel;
@property (nonatomic,strong)UILabel *secondLabel;
@property (nonatomic,strong)NoticeView *noview;
@property (nonatomic,strong)UIScrollView *vipScrollView;
@property (nonatomic,strong)NSArray *titleArray;
@property (nonatomic       )NSInteger pageScroll;
@property (nonatomic,strong)NSMutableArray *tableViewAry;
@property (nonatomic,strong)VipViewModel *viewModel;
@property (nonatomic,strong)NSDictionary *countDic;
@property (nonatomic,strong)NSArray *listAry;
@property (nonatomic,strong)NSArray *providersListAry;
@property (nonatomic,strong)ToolView *toolview;
@end

@implementation VipViewController

-(void)noviews {
    
    [_noview removeFromSuperview];

    _noview = [[NoticeView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, 44) andtitle:self.countDic andType:_pageScroll];
        [self.view addSubview:_noview];
  
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self noviews];
}
-(NSDictionary *)countDic {
    if (!_countDic) {
        _countDic = [NSDictionary dictionary];
    }
    return _countDic;
}
-(void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [_noview removeFromSuperview];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    _pageScroll = 0;

    [self navView];
    
    self.titleArray = @[@"会员列表",@"服务商列表"];
    _tableViewAry = [NSMutableArray array];
    
    _listAry = [NSArray array];
    
    _providersListAry = [NSArray array];
    
    _scrollOffSize =0;//随便设置的数字,占个坑

    
    [self.view addSubview:self.vipScrollView];

    _toolview  =[[ToolView alloc]initWithFrame:CGRectMake(0, 64+44, WIDTH, 44) andTitleAry:self.titleArray];
    @weakify(self);
    [_toolview setToolBlock:^(UIButton *btn) {
        self_weak_.vipScrollView.contentOffset = CGPointMake(WIDTH *btn.tag, 0)  ;
        self_weak_.pageScroll = btn.tag;
    }];
    _toolview.layer.borderWidth =0.5;
    _toolview.layer.borderColor = [ManagerEngine getColor:@"cccccc"].CGColor;

    
    /**
     监听滚动视图

     
     */
    [RACObserve(self, pageScroll)subscribeNext:^(id x) {
        NSLog(@"我是RAC 监听%@",x);
        if (_scrollOffSize != [x integerValue]) {
           
                    [self requstList];
           
            _scrollOffSize = [x integerValue];
        }
        
    }];
    [self.view addSubview:_toolview];
  
    
    [self createTableView];
    _viewModel = [[VipViewModel alloc]init];
    [self_weak_ requstList];
}

#pragma mark --
#pragma mark ---导航控制器上的按钮
-(void)navView {
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn bk_addEventHandler:^(UIButton *sender) {
        CGPoint point = CGPointMake(sender.center.x,sender.frame.origin.y + 55);

        XTPopView *popView = [[XTPopView alloc]initWithOrigin:point Width:140 Height:80 Type:XTTypeOfUpRight Color:[UIColor colorWithRed:0.2737 green:0.2737 blue:0.2737 alpha:1.0]];
        popView.dataArray = @[@"会员注册", @"服务商注册"];
        
        popView.images = @[@"member_register", @"store_register"];
        
        popView.fontSize = 13;
        
        popView.row_height = 40;
        
        popView.width = 140;
        
        popView.delegate = self;
        
        
        popView.titleTextColor = [UIColor whiteColor];
        
        [popView popViewWinthView:self.view];
        
    } forControlEvents:UIControlEventTouchUpInside];
    [btn setImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    btn.frame =CGRectMake(0, 0, 100, 100);
//    btn.backgroundColor = [UIColor greenColor];
    [btn sizeToFit];
//    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]initWithCustomView:btn];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    
    
}

#pragma mark --
#pragma mark ---右上角按钮菜单Deletage
-(void)selectIndexPathRow:(NSInteger)index {
    
    if (index==0) {
        logonVipViewController *LVVC = [[logonVipViewController alloc]init];
        LVVC.title = @"会员注册";
        [self.navigationController pushViewController:LVVC animated:YES];
    } else {
        FacilitatorLogonViewController *FVC = [[FacilitatorLogonViewController alloc]init];
        FVC.title = @"服务商注册";
        [self.navigationController pushViewController:FVC animated:YES];
        
    }
}

-(UIScrollView *)vipScrollView {
    if (!_vipScrollView) {
        _vipScrollView =[[UIScrollView alloc]init];
        _vipScrollView.frame = CGRectMake(0, 64+44+44, WIDTH, HEIGHT-64-44-44-44);
//        _vipScrollView.backgroundColor = [UIColor cyanColor];
        _vipScrollView.alwaysBounceHorizontal=YES;
        _vipScrollView.bounces = YES;
        _vipScrollView.pagingEnabled = YES;
        _vipScrollView.delegate = self;
        _vipScrollView.userInteractionEnabled = YES;
        _vipScrollView.showsHorizontalScrollIndicator = NO;
        _vipScrollView.showsVerticalScrollIndicator = NO;
        _vipScrollView.contentOffset = CGPointMake(0, 0);
        _vipScrollView.contentSize = CGSizeMake(WIDTH*[self.titleArray count], HEIGHT-64-44-44-44);
        [_vipScrollView scrollRectToVisible:CGRectMake(0,0,WIDTH,HEIGHT-64-44-44-44) animated:NO];
        
    }
    
    
    
    return _vipScrollView;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    
    if (scrollView == self.vipScrollView) {
        self.pageScroll = scrollView.contentOffset.x/WIDTH;
        [_toolview ClickBtn :self.pageScroll];
    }
}

-(void)createTableView {
    
    for (NSInteger i = 0; i<[self.titleArray count]; i++) {
        UITableView *table = [[UITableView alloc]init];
        
        table.tag = i;
        
        table.frame = CGRectMake(i%[self titleArray].count*WIDTH, 0, WIDTH,self.vipScrollView.frame.size.height);
        table.backgroundColor = [UIColor groupTableViewBackgroundColor];
        table.delegate = self;
        table.dataSource = self;
        [table registerClass:[UITableViewCell class] forCellReuseIdentifier:NSStringFromClass([UITableViewCell class])];
        [table registerClass:[VipTableViewCell class] forCellReuseIdentifier:NSStringFromClass([VipTableViewCell class])];
        table.tableFooterView = [UIView new];
        table.rowHeight = 70.f;
        [self.vipScrollView addSubview:table];
        table.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            [self requstList];
        }];
        
       // [table.mj_header beginRefreshing];


        [_tableViewAry addObject:table];
//    }

    }
    
    
    
    
    
}

#pragma mark --
#pragma mark ---
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView.tag == 0) {
        return _listAry.count;

    } else {
        return _providersListAry.count;
    }
    
    
}
#pragma mark --
#pragma mark --- tableView dataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    VipTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([VipTableViewCell class]) forIndexPath:indexPath];
    if (tableView.tag == 0) {
        cell.model = _listAry[indexPath.row];

    } else {
        cell.model = _providersListAry[indexPath.row];

    }
    [cell updateLayout];
    return cell;
    
    
    
}
-(void)requstList {
    @weakify(self)
  
    [_viewModel setCountBlock:^(NSDictionary * dic) {
        
        
        HQJLog(@"在控制器里面返回的会员数量:%@",dic);
        
        self_weak_.countDic = dic;
        [self_weak_ noviews];
        
    }];
    
    if (self.pageScroll== 0) {
        if (self_weak_.listAry.count==0) {
            [_viewModel setOriInfoBlock:^(NSArray *array) {
                
                //        HQJLog(@"在控制器里面返回其他一大堆:%@",array);
                if (self_weak_.pageScroll == 0) {
                    _listAry = array;
                    
                } else {
                    _providersListAry = array;
                }
                for (UITableView *tabelView in self_weak_.tableViewAry) {
                    
                    if (tabelView.tag == self_weak_.pageScroll) {
                        [tabelView.mj_header endRefreshing];
                        //
                        [tabelView reloadData];
                        
                    }
                }
            }];

        }
    } else {
        if (self_weak_.providersListAry.count==0) {
            [_viewModel setOriInfoBlock:^(NSArray *array) {
                
                //        HQJLog(@"在控制器里面返回其他一大堆:%@",array);
                if (self_weak_.pageScroll == 0) {
                    _listAry = array;
                    
                } else {
                    _providersListAry = array;
                }
                for (UITableView *tabelView in self_weak_.tableViewAry) {
                    
                    if (tabelView.tag == self_weak_.pageScroll) {
                        [tabelView.mj_header endRefreshing];
                        //
                        [tabelView reloadData];
                        
                    }
                }
            }];

        }
    }

    
    
    
    [_viewModel vipRequst:self.pageScroll andView:self.view];
    
    
    
    
}
@end
